/*
	This file is part of "Ants on the web".

    "Ants on the web" is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    "Ants on the web" is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with "Ants on the web".  If not, see <http://www.gnu.org/licenses/>.
*/

package org.utc.ia04.antsontheweb.utilities;

import java.io.Serializable;
import java.io.StringWriter;
import java.util.HashSet;

import org.codehaus.jackson.map.ObjectMapper;

/**
 * @author Ronan
 * @category Request
 * This class represents the content of a formatted query to knowledgeBase.
 * WARNING : must be encapsulated in utilities.Requete
 * 
 * Content :
 *      websiteType : see KnowledgeBaseAgent
 *      keywords : ArrayList of keywords
 */
public class KnowledgeBaseRequest implements Serializable
{
	//serialVersionUID
	private static final long serialVersionUID = -6067514557722590997L;
	
	private int websiteType;
	private HashSet<String> keywords;
	
	public KnowledgeBaseRequest()
	{
		websiteType = -1;
		keywords = null; 
	}
	
	public KnowledgeBaseRequest(int websiteType, HashSet<String> keywords)
	{
		this.websiteType = websiteType;
		this.keywords = keywords;
	}

	public void setWebsiteType(int websiteType)
	{
		this.websiteType = websiteType;
	}

	public int getWebsiteType()
	{
		return websiteType;
	}

	public void setKeywords(HashSet<String> keywords)
	{
		this.keywords = keywords;
	}

	public HashSet<String> getKeywords()
	{
		return keywords;
	}
	
	//Serialization/Deserialization
	public static KnowledgeBaseRequest deserialize(String s)
	{
		ObjectMapper m = new ObjectMapper();
		try{
			KnowledgeBaseRequest q = m.readValue(s, KnowledgeBaseRequest.class);
			return q;
		}catch (Exception ex){
			//System.out.println("Erreur de deserialization : " + ex.toString());
			return null;
		}
	}
	
	public String serialize()
	{
		ObjectMapper m = new ObjectMapper();
		StringWriter sw = new StringWriter();
		try{
			m.writeValue(sw, this);
			return sw.toString();
		}
		catch(Exception ex){
			//System.out.println("Erreur de serialization : " + ex.toString());
			return "";
		}
	}
}
